package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.dto.AddressBookDto;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.vo.AddressBookVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.AbstractDocument;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :AddressBookServiceImpl
 * creationTime :2022-11-28 12:33
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public AddressBookVo insertAddressBook(AddressBookDto addressBookDto) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto,addressBook);
        addressBook.setId(IdUtil.getSnowflakeNextIdStr());
        addressBook.setUserId(ThreadLocalUtil.getid());
        addressBook.setCreateUser(ThreadLocalUtil.getid());
        addressBook.setUpdateUser(ThreadLocalUtil.getid());
        Integer insert=addressBookMapper.insertAddressBook(addressBook);
        AddressBookVo addressBookVo=null;
        if (insert>0){
            addressBookVo = new AddressBookVo();
            BeanUtils.copyProperties(addressBook,addressBookVo);
        }
        return addressBookVo;
    }

    @Override
    public List<AddressBookVo> selectAddressBooks() {
        List<AddressBook> addressBooks=addressBookMapper.selectAddressBooks(ThreadLocalUtil.getid());
        List<AddressBookVo> addressBookVos = new ArrayList<>();
        for (AddressBook addressBook : addressBooks) {
            AddressBookVo addressBookVo = new AddressBookVo();
            BeanUtils.copyProperties(addressBook,addressBookVo);
            addressBookVos.add(addressBookVo);
        }
        return addressBookVos;
    }

    @Override
    public AddressBookVo selectAddressBookById(String id) {
        AddressBook addressBook=addressBookMapper.selectAddressBookById(id,ThreadLocalUtil.getid());
        AddressBookVo addressBookVo = new AddressBookVo();
        BeanUtils.copyProperties(addressBook,addressBookVo);
        return addressBookVo;
    }

    @Override
    public Boolean updateAddressBooks(AddressBookDto addressBookDto){
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto,addressBook);
        addressBook.setUserId(ThreadLocalUtil.getid());
        addressBook.setUpdateUser(ThreadLocalUtil.getid());
        Integer update=addressBookMapper.updateAddressBooks(addressBook);
        return update>0;
    }

    @Override
    public Boolean deleteAddressBooks(List<String> ids) {
        Integer delete=addressBookMapper.updateAddressBooksByIds(ids,ThreadLocalUtil.getid());
        return delete>0;
    }

    @Override
    public AddressBookVo setDefaultAddressBooks(AddressBookDto addressBookDto) {
        AddressBook addressBookInDB=addressBookMapper.selectByIsDefault(ThreadLocalUtil.getid());
        if (addressBookInDB!=null) {
            addressBookInDB.setIsDefault(0);
            addressBookMapper.setDefaultAddressBooks(addressBookInDB);
        }
        AddressBook addressBook = new AddressBook();
        addressBook.setId(addressBookDto.getId());
        addressBook.setUserId(ThreadLocalUtil.getid());
        addressBook.setIsDefault(1);
        Integer set=addressBookMapper.setDefaultAddressBooks(addressBook);
        if(set<=0){
            return null;
        }
        AddressBook addressBook1 = addressBookMapper.selectAddressBookById(addressBook.getId(), addressBook.getUserId());
        AddressBookVo addressBookVo = new AddressBookVo();
        BeanUtils.copyProperties(addressBook1,addressBookVo);
        return addressBookVo;
    }

    @Override
    public AddressBookVo selectDefaultAddressBook() {
        AddressBook addressBook = addressBookMapper.selectByIsDefault(ThreadLocalUtil.getid());
        if(addressBook==null){
            return null;
        }
        AddressBookVo addressBookVo = new AddressBookVo();
        BeanUtils.copyProperties(addressBook,addressBookVo);
        return addressBookVo;
    }


}
